package com.huaxin.common.view;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.ListView;

/**
 * Created by hebing on 2017/1/16.
 */

public class ScrollListView extends ListView {
    public ScrollListView(Context context) {
        super(context);
    }

    public ScrollListView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ScrollListView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public ScrollListView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        if (callback != null && callback.isEnable()) {
            if (callback.isScroll()) {
                //禁止自身滑动
                return callback.onTouchEvent(ev);
            } else {
                //已打开时，触摸隐藏
//                if (callback.isOpen()) {
//                    callback.close();
//                    return false;
//                }
            }
        }
        //默认流程
        return super.onTouchEvent(ev);
    }

    public interface ScrollCallback {
        boolean isEnable();

        boolean isOpen();//是否打开

        void close();//恢复初始状态

        boolean isScroll();//是否正在滑动

        boolean onTouchEvent(MotionEvent ev);//链式回调
    }

    ScrollCallback callback;

    public void setCallback(ScrollCallback callback) {
        this.callback = callback;
    }

    public ScrollCallback getCallback() {
        return callback;
    }
}
